package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.list.model.SearchAttr;
import com.atguigu.gmall.list.model.SearchParam;
import com.atguigu.gmall.list.model.SearchResponseVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-01-07 15:33
 */
@Controller
public class ListController {


    @Autowired
    private ListFeignClient listFeignClient;


    /**
     * 门户页面中商品检索页面渲染
     *
     * @param searchParam
     * @param model
     * @return
     */
    @GetMapping("/list.html")
    public String list(SearchParam searchParam, Model model) {
        //1.远程调用搜索服务获取检索数据
        Result<Map> result = listFeignClient.search(searchParam);

        //2.给页面模板中数据模型赋值
        model.addAllAttributes(result.getData());
        //2.1 用户选中品牌面包屑返回，用于页面品牌面包屑展示
        String trademarkParam = this.makeTrademark(searchParam.getTrademark());
        model.addAttribute("trademarkParam", trademarkParam);
        //2.2 用户选中搜索过滤条件-平台数据，用于页面平台属性面包屑展示 propsParamList
        List<SearchAttr> propsParamList = this.makePropsParamList(searchParam.getProps());
        model.addAttribute("propsParamList", propsParamList);
        //2.3 用户在地址栏中请求完整URL需要拼接返回，用于页面浏览器地址栏中展示 urlParam
        String urlParam = this.makeUrlParam(searchParam);
        model.addAttribute("urlParam", urlParam);
        //2.4 用户所有搜索条件对象SearchParam返回
        model.addAttribute("searchParam", searchParam);
        //2.5 根据前端选择排序-页面中需要选中排序样式
        Map<String, String> orderMap = this.makeOrderMap(searchParam.getOrder());
        model.addAttribute("orderMap", orderMap);
        //3.返回页面模板进行渲染
        return "list/index.html";
    }

    /**
     * 根据用户选择平台属性，封装页面中显示平台属性面包屑
     *
     * @param props
     * @return
     */
    private List<SearchAttr> makePropsParamList(String[] props) {
        if (props != null && props.length != 0) {
            List<SearchAttr> searchAttrsList = new ArrayList<>();
            for (String prop : props) {
                SearchAttr searchAttr = new SearchAttr();
                String[] split = prop.split(":");
                if (split != null && split.length == 3) {
                    searchAttr.setAttrId(Long.parseLong(split[0]));
                    searchAttr.setAttrValue(split[1]);
                    searchAttr.setAttrName(split[2]);
                    searchAttrsList.add(searchAttr);
                }
            }
            return searchAttrsList;
        }
        return null;
    }

    /**
     * 在搜索页面中显示用户已选品牌面包屑
     *
     * @param trademark
     * @return
     */
    private String makeTrademark(String trademark) {
        if (StringUtils.isNotBlank(trademark)) {
            String[] split = trademark.split(":");
            return "品牌：" + split[1];
        }
        return null;
    }


    /**
     * 根据前端提交的条件对象封装完整的URL地址
     *
     * @param searchParam
     * @return
     */
    private String makeUrlParam(SearchParam searchParam) {
        //1.构建最基础的请求地址字符串 /list.html?
        StringBuilder urlStringBuilder = new StringBuilder("/list.html?");
        //2.处理分类参数
        if (searchParam.getCategory1Id() != null) {
            urlStringBuilder.append("&category1Id=").append(searchParam.getCategory1Id());
        }
        if (searchParam.getCategory2Id() != null) {
            urlStringBuilder.append("&category2Id=").append(searchParam.getCategory2Id());
        }
        if (searchParam.getCategory3Id() != null) {
            urlStringBuilder.append("&category3Id=").append(searchParam.getCategory3Id());
        }

        //3.处理品牌参数
        if (StringUtils.isNotBlank(searchParam.getTrademark())) {
            String[] split = searchParam.getTrademark().split(":");
            if (split != null && split.length == 2) {
                urlStringBuilder.append("&trademark=").append(searchParam.getTrademark());
            }
        }
        //4.处理关键字参数
        if (StringUtils.isNotBlank(searchParam.getKeyword())) {
            urlStringBuilder.append("&keyword=" + searchParam.getKeyword());
        }

        //5.处理排序参数
        if (StringUtils.isNotBlank(searchParam.getOrder())) {
            urlStringBuilder.append("&order=").append(searchParam.getOrder());
        }

        //6.处理平台属性参数
        String[] props = searchParam.getProps();
        if (props != null && props.length != 0) {
            for (String prop : props) {
                String[] split = prop.split(":");
                if (split != null && split.length == 3) {
                    urlStringBuilder.append("&props=").append(prop);
                }
            }
        }
        return urlStringBuilder.toString();
    }


    /**
     * 根据用户选择排序返回选中排序Map
     *
     * @param order
     * @return
     */
    private Map<String, String> makeOrderMap(String order) {
        HashMap<String, String> orderMap = new HashMap<>();
        if (StringUtils.isNotBlank(order)) {
            //前端选择排序方式 参数形式：&order=2:desc
            String[] split = order.split(":");
            if (split != null && split.length == 2) {
                orderMap.put("type", split[0]);
                orderMap.put("sort", split[1]);
                return orderMap;
            }
        } else {
            //前端没有选择 ES检索默认按照热点商品降序
            orderMap.put("type", "1");
            orderMap.put("sort", "desc");
        }
        return orderMap;
    }
}
